/*****************************************************************************
 * ui_application.c: Copyright 2023 (c) CoreLink
 * Desc:
 *
 * Author: Flying (weekend@111.com)
 * Date: 2023-09-06 12:10:57
 * Notes:
 *
 * ---------------------------------------------------------------------------
 * LastEditors:
 * LastEditTime:
 *****************************************************************************/

/*------------------------------- Includes ----------------------------------*/
#include "ui_application.h"
#include "my_conf.h"
#include "my_def.h"
#include <string.h>
#include "ui_sys_head.h"
#include "ui_sys_home_btn.h"
#include "ui_sys_body.h"
#include "ui_sys_explain.h"
#include "ui_sys_footer.h"
#include "ui_sys_settings.h"
#include "ui_sys_quick_win.h"
#include "app.h"
// #include "cl_mb_common.h"

/*------------------- 全局定义和声明 -------------------*/

/*---------------------- 常量/宏定义 -----------------------*/

/*----------------------- 类型声明 ---------------------------------*/

typedef struct apps_func_
{
    const lv_img_dsc_t *img_dsc;
    const char *label;
    app_fun_t func;
    quick_win_fun_t quick_func;
} apps_func_t;

typedef struct ui_app_info_t_
{
    lv_obj_t *app_home;
    lv_obj_t *loading;
    int app_create_cnt;
    char is_theme_dark : 1;
    char is_app : 1;
    ui_sys_head_info_t *head_info;
    ui_sys_home_btn_info_t *home_btn_info;
    ui_sys_body_info_t *body_info;
    ui_sys_footer_info_t *footer_info;
    ui_sys_explain_info_t *explain_info;
    ui_sys_settings_info_t *settings_info;
} ui_app_info_t;

/*----------------------- 变量声明 -----------------------------*/
static apps_func_t apps[] = {
#include "app_fun_list.hh"
};

#define APPS_SIZE (sizeof(apps) / sizeof(apps[0]))

static ui_app_info_t g_info;
/*----------------------- 函数原型 --------------------------------*/
static void home_btn_event_cb(int code, void *cb_arg);
static void footer_event_cb(int index, void *cb_arg);
static void ui_app_show_app(lv_obj_t *cont);
static void ui_app_body_event_cb(ui_sys_body_info_t *body_info);
static bool ui_app_del_app();
static void ui_app_refresh_footer(int index);
static void ui_app_change_theme();

/*----------------------- 功能实现 --------------------------------*/

void ui_application_init()
{
    LV_IMG_DECLARE(bg1);

    memset(&g_info, 0, sizeof(ui_app_info_t));
    // .head = LIST_HEAD_INIT(g_transport_list.head)
    g_info.app_home = lv_img_create(NULL);
    lv_obj_remove_style_all(g_info.app_home);
    lv_img_set_size_mode(g_info.app_home, LV_IMG_SIZE_MODE_REAL);
    lv_img_set_src(g_info.app_home, &bg1);
    lv_obj_set_size(g_info.app_home, MY_UI_W_MAX, MY_UI_H_MAX);
    lv_obj_clear_flag(g_info.app_home, LV_OBJ_FLAG_SCROLLABLE);

    // // 创建head
    // g_info.head_info = ui_sys_head_create(g_info.app_home);
    // ui_sys_head_show(g_info.head_info);

    // // 创建home btn
    // g_info.home_btn_info = ui_sys_home_btn_create((home_btn_event_cb_t)home_btn_event_cb, (void *)&g_info);
    // ui_sys_home_btn_show(g_info.home_btn_info);
    // 创建body
    g_info.body_info = ui_sys_body_create(g_info.app_home, ui_app_body_event_cb, NULL);
    ui_sys_body_show(g_info.body_info);

    // // 创建  explain
    // g_info.explain_info = ui_sys_explain_create(lv_layer_top());

    // // 创建  settings
    // g_info.settings_info = ui_sys_settings_create(lv_layer_top());

    lv_disp_load_scr(g_info.app_home);
    while (ui_application_run())
    {
    };
    PR_DEBUG("demo hub success");
}

/**
 * @description:
 * @param {int} index
 * @return {*}
 */
static void footer_event_cb(int index, void *cb_arg)
{
    // PR_DEBUG("footer_event_cb: index = %d\n", index);

    ui_sys_body_select_page(g_info.body_info, index, true);
}

/**
 * @description:
 * @param {int} code
 * @param {void} *cb_arg
 * @return {*}
 */
static void home_btn_event_cb(int code, void *cb_arg)
{
    bool is = false;
    switch (code)
    {
    case HOME_BTN_CODE_HOME:
        ui_sys_head_reset(g_info.head_info, LV_ANIM_ON);
        is = ui_app_del_app();
        if (ui_sys_settings_exit(g_info.settings_info))
        {
            is = true;
        }
        if (ui_sys_explain_exit(g_info.explain_info))
        {
            is = true;
        }
        if (!is)
        {
            ui_sys_body_select_page(g_info.body_info, 0, LV_ANIM_ON);
        }
        break;
    case HOME_BTN_CODE_SETTINGS:
        is = ui_sys_explain_exit(g_info.explain_info);
        is = ui_sys_settings_exit(g_info.settings_info);
        if (is)
        {
            break;
        }
        ui_sys_settings_show(g_info.settings_info);
        break;
    case HOME_BTN_CODE_THEME:
        ui_app_change_theme();
        break;
    case HOME_BTN_CODE_EXPLAIN:

        is = ui_sys_settings_exit(g_info.settings_info);
        is = ui_sys_explain_exit(g_info.explain_info);
        if (is)
        {
            break;
        }
        ui_sys_explain_show(g_info.explain_info);
        break;
    default:
        break;
    }
}

int ui_application_run()
{
    if (g_info.app_create_cnt < APPS_SIZE)
    {
        apps_func_t *p = (apps_func_t *)&apps[g_info.app_create_cnt];
        ui_sys_body_add_item(g_info.body_info, p->img_dsc, (char *)p->label, p->quick_func ? true : false, p->func ? true : false);
        g_info.app_create_cnt++;
        return 1;
    }
    else if (g_info.app_create_cnt == APPS_SIZE)
    {
        g_info.app_create_cnt++;
        int count = 0;
        LIST_HEAD *node = NULL;

        tuya_list_for_each(node, &(g_info.body_info->page_list))
        {
            count++;
        }
        // 创建footer
        g_info.footer_info = ui_sys_footer_create(g_info.app_home, count, footer_event_cb, (void *)&g_info);
        ui_sys_footer_show(g_info.footer_info);
        ui_sys_footer_set_bg_color(g_info.footer_info, lv_color_hex(0xffffff));

        // 创建home btn
        if (!g_info.home_btn_info)
        {
            PR_DEBUG("create home_btn_info\n");
            g_info.home_btn_info = ui_sys_home_btn_create((home_btn_event_cb_t)home_btn_event_cb, (void *)&g_info);
            ui_sys_home_btn_show(g_info.home_btn_info);
        }

        // 创建head
        if (!g_info.head_info)
        {
            PR_DEBUG("create head_info\n");
            g_info.head_info = ui_sys_head_create(g_info.app_home);
            ui_sys_head_show(g_info.head_info);
        }

        // 创建  settings
        if (!g_info.settings_info)
        {
            PR_DEBUG("create settings_info\n");
            g_info.settings_info = ui_sys_settings_create(lv_layer_top());
        }

        // 创建  explain
        if (!g_info.explain_info)
        {
            PR_DEBUG("create explain_info\n");
            g_info.explain_info = ui_sys_explain_create(lv_layer_top());
        }
    }
    return 0;
}

/**
 * @description:
 * @param {int} index
 * @return {*}
 */
static void ui_app_body_event_cb(ui_sys_body_info_t *body_info)
{

    SYS_BODY_CODE_E code = ui_sys_body_get_sync_code(body_info);
    switch (code)
    {
    case SYS_BODY_CODE_SCROLL:
        ui_app_refresh_footer(body_info->sync_page);
        break;
    case SYS_BODY_CODE_CLICKED:
        if (ui_sys_body_get_sync_index(body_info) < APPS_SIZE)
        {
            if (!apps[ui_sys_body_get_sync_index(body_info)].func)
            {
                LV_LOG_ERROR(" not func ");
                return;
            }
            lv_obj_t *app = apps[ui_sys_body_get_sync_index(body_info)].func();
            if (!app)
            {
                return;
            }
            ui_app_show_app(app);
        }
        break;
    case SYS_BODY_CODE_GESTURE_UP:
    {
        sys_quick_func_t func = NULL;
        LV_LOG_USER("%d , %ld", ui_sys_body_get_sync_index(g_info.body_info), APPS_SIZE);

        if (ui_sys_body_get_sync_index(g_info.body_info) < APPS_SIZE)
        {
            func = apps[ui_sys_body_get_sync_index(g_info.body_info)].quick_func;
        }
        if (!func)
        {
            LV_LOG_ERROR(" not func ");
            return;
        }
        lv_obj_t *obj = NULL;
        obj = ui_sys_body_get_item_obj(g_info.body_info, ui_sys_body_get_sync_index(g_info.body_info));
        if (!obj)
        {
            LV_LOG_ERROR("ui_sys_body_get_item_obj error");
            return;
        }
        lv_area_t area;
        lv_obj_get_click_area(obj, &area);
        int x = (area.x2 - area.x1) / 2 + area.x1;
        int y = (area.y2 - area.y1) / 2 + area.y1;
        ui_sys_quick_win_create(x, y, func);
    }

    break;
    default:
        break;
    }
}

/**
 * @description:
 * @param {*}
 * @return {*}
 */
static void ui_app_show_app(lv_obj_t *cont)
{
    static int anim_type = LV_SCR_LOAD_ANIM_NONE;

    g_info.is_app = true;

    lv_scr_load_anim(cont, (lv_scr_load_anim_t)anim_type, 20, 0, false);
    // if (++anim_type >= LV_SCR_LOAD_ANIM_OUT_BOTTOM)
    // {
    //     anim_type = LV_SCR_LOAD_ANIM_OVER_LEFT;
    // }
}

static void ui_app_refresh_footer(int index)
{
    if (!g_info.footer_info)
    {
        return;
    }
    ui_sys_footer_select(g_info.footer_info, index);
}

/**
 * @description:
 * @param {*}
 * @return {*}
 */
static bool ui_app_del_app()
{
    static int anim_type = LV_SCR_LOAD_ANIM_NONE;
    if (!g_info.is_app)
    {
        return false;
    }
    g_info.is_app = false;
    lv_scr_load_anim(g_info.app_home, (lv_scr_load_anim_t)anim_type, 20, 0, true);

    // if (++anim_type >= LV_SCR_LOAD_ANIM_OUT_BOTTOM)
    // {
    //     anim_type = LV_SCR_LOAD_ANIM_OVER_LEFT;
    // }
    return true;
}

/**
 * @description: 主题切换
 * @param {*}
 * @return {*}
 */
static void ui_app_change_theme()
{
    LV_IMG_DECLARE(bg1);
    LV_IMG_DECLARE(bg2);
    g_info.is_theme_dark = !g_info.is_theme_dark;
    if (g_info.is_theme_dark)
    {
        // lv_obj_set_style_bg_img_src(g_info.app_home, MY_PNG_PATH "dark_bg.png", 0);
        lv_img_set_src(g_info.app_home, &bg2);
        lv_theme_default_init(NULL, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED),
                              1, LV_FONT_DEFAULT);
        ui_sys_footer_set_bg_color(g_info.footer_info, lv_color_hex(0x6f6f6f));
    }
    else
    {
        lv_img_set_src(g_info.app_home, &bg1);
        // lv_obj_set_style_bg_img_src(g_info.app_home, MY_PNG_PATH "bg.png", 0);
        lv_theme_default_init(NULL, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED),
                              0, LV_FONT_DEFAULT);
        ui_sys_footer_set_bg_color(g_info.footer_info, lv_color_hex(0xffffff));
    }
}
/*---------------------------------------------------------------------------*/